import { message } from 'antd';
import * as api from '../utils/api';

const MESSAGEERROR = message.error

export default {

    namespace: 'role',

    state: {
        dataSource: [],
        current: 1,
        totalPage: 0,
        modal: {},
        visible: false,
        confirmLoading: false,
        columns : [
            {
                title: 'id',
                dataIndex: 'id',
                key: 'id',
                width:'10%'
            },
            {
                title: '名称',
                dataIndex: 'name',
                key: 'name'
            },
            {
                title: '备注',
                dataIndex: 'remarks',
                key: 'remarks'
            },
            {
                title: '创建时间',
                dataIndex: 'createTime',
                key: 'createTime'
            }
        ],
    },
    effects: {
        *getDataSource({ payload }, { call, put }) {
            const res = yield call(api.getRoleTable, payload);
            console.log(res)
            if (res.code === 0) {
                yield put({
                    type: 'updateDataSource',
                    action: Object.assign(res.data, payload.data)
                })
            } else {
                MESSAGEERROR('(︶︿︶)' + res.msg)
            }
        }
    },
    reducers: {
        'updateDataSource'(state, { action: data }) {
            let newState = Object.assign({}, state)
            console.log()
            newState.dataSource = data.list
            newState.totalPage = data.totalPage
            newState.currPage = data.currPage
            return newState
        },
        'getSearch'(state, { action: name }) {
            let newState = Object.assign({}, state)
            newState.dataSource = newState.dataSource.filter(item => item.name === name)
            return newState
        },
        'editDataSource'(state, { action: id }) {
            let newState = Object.assign({}, state)
            newState.dataSource = newState.dataSource.filter(item => item.id !== id)
            return newState
        },
        'showModal'(state, { action = {} }) {
            let newState = Object.assign({}, state)
            newState.visible = !action.visible
            console.log('action',action)
            newState.modal = !action.id ? {} : newState.dataSource.filter(item => item.id === action.id)[0]
            return newState
        },
        'handleOk'(state, { action: data }) {
            let newState = Object.assign({}, state)
            newState.confirmLoading = true
            newState.visible = false
            newState.confirmLoading = false
            const { res, fun  } = data
            switch (fun) {
                case 'upDate':
                    newState.dataSource = newState.dataSource.map(item => item.id === res.id ? res : item)
                    break;
                case 'add':
                    res.id = newState.dataSource.length + 1
                    newState.dataSource.push(res)
                    break;
                default:
                    break;
            }
            newState.modal = {}
            return newState
        }
    },
};
